package com.ygl.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.ygl.common.api.BaseApiService;
import com.ygl.constants.BaseApiConstants;
import com.ygl.entity.UserEntity;
import com.ygl.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author： ygl
 * @date： 2019/02/03
 * @Description：
 */
@Controller
public class BaseController  extends BaseApiService {
    public static final String ERROR = "common/error";
    @Autowired
    private UserFeign userFeign;

    public UserEntity getUserEntity(String token) {
        Map<String, Object> userMap = userFeign.getUser(token);
        Integer code = (Integer) userMap.get(BaseApiConstants.HTTP_CODE_NAME);
        if (!code.equals(BaseApiConstants.HTTP_200_CODE)) {
            return null;
        }
        // 获取data参数
        LinkedHashMap linkedHashMap = (LinkedHashMap) userMap.get(BaseApiConstants.HTTP_DATA_NAME);
        String json = new JSONObject().toJSONString(linkedHashMap);
        UserEntity userEntity = new JSONObject().parseObject(json, UserEntity.class);
        return userEntity;

    }

    public String setError(HttpServletRequest request, String msg, String addres) {
        request.setAttribute("error", msg);
        return addres;
    }

}